package cn.liwq.study.demo.netty.lab.mahjong.common.enums;

import cn.liwq.study.demo.netty.lab.mahjong.common.dto.CreateRoomRequest;
import cn.liwq.study.demo.netty.lab.mahjong.common.dto.CreateRoomResponse;
import cn.liwq.study.demo.netty.lab.mahjong.common.dto.EnterRoomRequest;
import cn.liwq.study.demo.netty.lab.mahjong.common.dto.EnterRoomResponse;
import cn.liwq.study.demo.netty.lab.mahjong.common.dto.GameOverNotificaion;
import cn.liwq.study.demo.netty.lab.mahjong.common.dto.HelloRequest;
import cn.liwq.study.demo.netty.lab.mahjong.common.dto.HelloResponse;
import cn.liwq.study.demo.netty.lab.mahjong.common.dto.LoginRequest;
import cn.liwq.study.demo.netty.lab.mahjong.common.dto.LoginResponse;
import cn.liwq.study.demo.netty.lab.mahjong.common.dto.OperationNotification;
import cn.liwq.study.demo.netty.lab.mahjong.common.dto.OperationRequest;
import cn.liwq.study.demo.netty.lab.mahjong.common.dto.OperationResultNotification;
import cn.liwq.study.demo.netty.lab.mahjong.common.dto.RoomRefreshNotification;
import cn.liwq.study.demo.netty.lab.mahjong.common.dto.SettlementNotification;
import cn.liwq.study.demo.netty.lab.mahjong.common.protocol.MahjongMessage;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author lweiqiang
 * @date 2021/5/8
 */
@Slf4j
@Getter
@AllArgsConstructor
public enum MessageManager {
    /** 请求 */
    HELLO_REQUEST(1, HelloRequest.class),
    /** 请求反馈 */
    HELLO_RESPONSE(2, HelloResponse.class),
    LOGIN_REQUEST(3, LoginRequest.class),
    LOGIN_RESPONSE(4, LoginResponse.class),
    CREATE_ROOM_REQUEST(5, CreateRoomRequest.class),
    CREATE_ROOM_RESPONSE(6, CreateRoomResponse.class),
    ENTER_ROOM_REQUEST(7, EnterRoomRequest.class),
    ENTER_ROOM_RESPONSE(8, EnterRoomResponse.class),
    ROOM_REFRESH_NOTIFICATION(9, RoomRefreshNotification.class),
    OPERATION_NOTIFICATION(10, OperationNotification.class),
    OPERATION_REQUEST(11, OperationRequest.class),
    OPERATION_RESULT_NOTIFICATION(12, OperationResultNotification.class),
    GAME_OVER_NOTIFICATION(13, GameOverNotificaion.class),
    SETTLEMENT_NOTIFICATION(14, SettlementNotification.class),
    ;

    private int cmd;

    private Class<? extends MahjongMessage> type;

    public static Class<? extends MahjongMessage> getTypeByCmd(int cmd) {
        for (MessageManager value : values()) {
            if (cmd == value.getCmd()) {
                return value.getType();
            }
        }
        log.error("error cmd: {}", cmd);
        throw new RuntimeException("error cmd");
    }

    public static int getCmdByMsgType(Class<? extends MahjongMessage> msgType) {
        for (MessageManager value : MessageManager.values()) {
            if (value.type == msgType) {
                return value.cmd;
            }
        }
        log.error("error msgType: {}", msgType.getName());
        throw new RuntimeException("error msgType: " + msgType.getName());
    }
}
